package com.bifang.core.dict.util;

import com.bifang.common.exception.AppException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.apache.commons.lang3.StringUtils;

/**
 * @author shun
 * @date 2022/02/23 下午10:44
 * @desc Pager转换工具
 */
public class SysDateUtil {

    public static Date getCurrentDate(String type) {
        Date date = new Date();
        if (StringUtils.isEmpty(type)) {
            throw new AppException("类型不能为空");
        }
        // 默认精确到秒
        String simpleType = "yyyy/MM/dd HH:mm:ss";
        if ("year".equals(type) || "YEAR".equals(type)) {
            simpleType = "yyyy";
        }
        if ("month".equals(type) || "MONTH".equals(type)) {
            simpleType = "yyyy/MM";
        }
        if ("day".equals(type) || "DAY".equals(type)) {
            simpleType = "yyyy/MM/dd";
        }
        if ("hour".equals(type) || "HOUR".equals(type)) {
            simpleType = "yyyy/MM/dd HH";
        }
        if ("hour".equals(type) || "HOUR".equals(type)) {
            simpleType = "yyyy/MM/dd HH";
        }
        if ("minute".equals(type) || "MINUTE".equals(type)) {
            simpleType = "yyyy/MM/dd HH:mm";
        }
        if ("second".equals(type) || "SECOND".equals(type)) {
            simpleType = "yyyy/MM/dd HH:mm:ss";
        }
        SimpleDateFormat sdf = new SimpleDateFormat(simpleType);
        String dateStr = sdf.format(date);
        Date result = null;
        try {
            result = sdf.parse(dateStr);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return result;
    }
}
